package model

import (
	"fmt"
	"gorm.io/gorm"
	"time"
)

type _WkbGroupMgr struct {
	*_BaseMgr
}

// WkbGroupMgr open func
func WkbGroupMgr(db *gorm.DB) *_WkbGroupMgr {
	if db == nil {
		panic(fmt.Errorf("WkbGroupMgr need init by db"))
	}
	ctx, cancel := context.WithCancel(context.Background())
	return &_WkbGroupMgr{_BaseMgr: &_BaseMgr{DB: db.Table("wkb_im_group"), isRelated: globalIsRelated, ctx: ctx, cancel: cancel, timeout: -1}}
}

// GetTableName get sql table name.获取数据库名字
func (obj *_WkbGroupMgr) GetTableName() string {
	return "wkb_im_group"
}

// Get 获取
func (obj *_WkbGroupMgr) Get() (result WkbGroup, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Find(&result).Error

	return
}

// Gets 获取批量结果
func (obj *_WkbGroupMgr) Gets() (results []*WkbGroup, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Find(&results).Error

	return
}

//////////////////////////option case ////////////////////////////////////////////

// WithID id获取 id
func (obj *_WkbGroupMgr) WithID(id uint32) Option {
	return optionFunc(func(o *options) { o.query["id"] = id })
}

// WithGroupID group_id获取 用户雪花ID，采用雪花算法
func (obj *_WkbGroupMgr) WithGroupID(groupID string) Option {
	return optionFunc(func(o *options) { o.query["group_id"] = groupID })
}

// WithName name获取 群名称
func (obj *_WkbGroupMgr) WithName(name string) Option {
	return optionFunc(func(o *options) { o.query["name"] = name })
}

// WithState state获取 0初始状态,1进行中,2完成
func (obj *_WkbGroupMgr) WithState(state int8) Option {
	return optionFunc(func(o *options) { o.query["state"] = state })
}

// WithNotice notice获取 群公告
func (obj *_WkbGroupMgr) WithNotice(notice string) Option {
	return optionFunc(func(o *options) { o.query["notice"] = notice })
}

// WithUpdatedAt updated_at获取 更新时间
func (obj *_WkbGroupMgr) WithUpdatedAt(updatedAt time.Time) Option {
	return optionFunc(func(o *options) { o.query["updated_at"] = updatedAt })
}

// WithCreatedAt created_at获取 创建时间
func (obj *_WkbGroupMgr) WithCreatedAt(createdAt time.Time) Option {
	return optionFunc(func(o *options) { o.query["created_at"] = createdAt })
}

// GetByOption 功能选项模式获取
func (obj *_WkbGroupMgr) GetByOption(opts ...Option) (result WkbGroup, err error) {
	options := options{
		query: make(map[string]interface{}, len(opts)),
	}
	for _, o := range opts {
		o.apply(&options)
	}

	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where(options.query).Find(&result).Error

	return
}

// GetByOptions 批量功能选项模式获取
func (obj *_WkbGroupMgr) GetByOptions(opts ...Option) (results []*WkbGroup, err error) {
	options := options{
		query: make(map[string]interface{}, len(opts)),
	}
	for _, o := range opts {
		o.apply(&options)
	}

	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where(options.query).Find(&results).Error

	return
}

//////////////////////////enume case ////////////////////////////////////////////

// GetFromID 通过id获取内容 id
func (obj *_WkbGroupMgr) GetFromID(id uint32) (result WkbGroup, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("id = ?", id).Find(&result).Error

	return
}

// GetBatchFromID 批量唯一主键查找 id
func (obj *_WkbGroupMgr) GetBatchFromID(ids []uint32) (results []*WkbGroup, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("id IN (?)", ids).Find(&results).Error

	return
}

// GetFromGroupID 通过group_id获取内容 用户雪花ID，采用雪花算法
func (obj *_WkbGroupMgr) GetFromGroupID(groupID string) (result WkbGroup, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("group_id = ?", groupID).Find(&result).Error

	return
}

// GetBatchFromGroupID 批量唯一主键查找 用户雪花ID，采用雪花算法
func (obj *_WkbGroupMgr) GetBatchFromGroupID(groupIDs []string) (results []*WkbGroup, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("group_id IN (?)", groupIDs).Find(&results).Error

	return
}

// GetFromName 通过name获取内容 群名称
func (obj *_WkbGroupMgr) GetFromName(name string) (results []*WkbGroup, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("name = ?", name).Find(&results).Error

	return
}

// GetBatchFromName 批量唯一主键查找 群名称
func (obj *_WkbGroupMgr) GetBatchFromName(names []string) (results []*WkbGroup, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("name IN (?)", names).Find(&results).Error

	return
}

// GetFromState 通过state获取内容 0初始状态,1进行中,2完成
func (obj *_WkbGroupMgr) GetFromState(state int8) (results []*WkbGroup, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("state = ?", state).Find(&results).Error

	return
}

// GetBatchFromState 批量唯一主键查找 0初始状态,1进行中,2完成
func (obj *_WkbGroupMgr) GetBatchFromState(states []int8) (results []*WkbGroup, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("state IN (?)", states).Find(&results).Error

	return
}

// GetFromNotice 通过notice获取内容 群公告
func (obj *_WkbGroupMgr) GetFromNotice(notice string) (results []*WkbGroup, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("notice = ?", notice).Find(&results).Error

	return
}

// GetBatchFromNotice 批量唯一主键查找 群公告
func (obj *_WkbGroupMgr) GetBatchFromNotice(notices []string) (results []*WkbGroup, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("notice IN (?)", notices).Find(&results).Error

	return
}

// GetFromUpdatedAt 通过updated_at获取内容 更新时间
func (obj *_WkbGroupMgr) GetFromUpdatedAt(updatedAt time.Time) (results []*WkbGroup, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("updated_at = ?", updatedAt).Find(&results).Error

	return
}

// GetBatchFromUpdatedAt 批量唯一主键查找 更新时间
func (obj *_WkbGroupMgr) GetBatchFromUpdatedAt(updatedAts []time.Time) (results []*WkbGroup, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("updated_at IN (?)", updatedAts).Find(&results).Error

	return
}

// GetFromCreatedAt 通过created_at获取内容 创建时间
func (obj *_WkbGroupMgr) GetFromCreatedAt(createdAt time.Time) (results []*WkbGroup, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("created_at = ?", createdAt).Find(&results).Error

	return
}

// GetBatchFromCreatedAt 批量唯一主键查找 创建时间
func (obj *_WkbGroupMgr) GetBatchFromCreatedAt(createdAts []time.Time) (results []*WkbGroup, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("created_at IN (?)", createdAts).Find(&results).Error

	return
}

//////////////////////////primary index case ////////////////////////////////////////////

// FetchByPrimaryKey primay or index 获取唯一内容
func (obj *_WkbGroupMgr) FetchByPrimaryKey(id uint32) (result WkbGroup, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("id = ?", id).Find(&result).Error

	return
}

// FetchUniqueByGroupID primay or index 获取唯一内容
func (obj *_WkbGroupMgr) FetchUniqueByGroupID(groupID string) (result WkbGroup, err error) {
	err = obj.DB.WithContext(obj.ctx).Table(obj.GetTableName()).Where("group_id = ?", groupID).Find(&result).Error

	return
}
